<?php

class IndexModel extends CommonModel
{
    function sidebar()
    {
        $result = [];
        //所有的栏目
        $result['categories'] = $this->all("select * from category");

        //最新日志
        $result['last_articles'] = $this->all("select * from article order by time desc limit 0, 3");

        //最新评论
        $result['last_comments'] = $this->all("select * from comment order by id desc limit 0,3");

        return $result;
    }

    function all_articles()
    {
        $articles = $this->all("select * from article");
        foreach ($articles as &$v) {
            $category_id = $v['category_id'];
            $category = $this->one("select * from `category` where `id`='$category_id'");
            $v['category'] = $category;
        }

        return $articles;
    }

    function store_article()
    {
        if ($_POST) {
            $title = $_POST['title'];
            $content = $_POST['content'];
            $category_id = $_POST['category_id'];
            $time = time();
            $this->db->query("insert into `article` (`category_id`, `title`, `content`,`time` ) values ('$category_id', '$title', '$content', '$time')");

            if ($this->db->affected_rows > 0) {
                return true;
            }
            return false;
        }
    }
}